/*//////////////////////////////////////////////////////////////////////////////
//
//                  INTEL CORPORATION PROPRIETARY INFORMATION
//     This software is supplied under the terms of a license agreement or
//     nondisclosure agreement with Intel Corporation and may not be copied
//     or disclosed except in accordance with the terms of that agreement.
//          Copyright(c) 2003-2007 Intel Corporation. All Rights Reserved.
//
*/

#include "umc_defs.h"
#if defined (UMC_ENABLE_MP3_AUDIO_DECODER) || defined (UMC_ENABLE_MP3_INT_AUDIO_DECODER) || defined (UMC_ENABLE_MP3_AUDIO_ENCODER) || defined (UMC_ENABLE_MP3_INT_AUDIO_ENCODER)

#include "mp3_own.h"

/* MPEG-1 bitrate tables: (p.21 ISO/IEC 11172-3) */
Ipp32s mp3_bitrate[2][3][16] = {
  { /* MPEG 2 */
    {0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256}, /* Layer 1 */
    {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160},      /* Layer 2 */
    {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160}       /* Layer 3 */
  } ,
  { /* MPEG 1 */
    {0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448}, /* Layer 1 */
    {0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384},    /* Layer 2 */
    {0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320}      /* Layer 3 */
  }
};

/* MPEG-1 sampling rates (p.21 ISO/IEC 11172-3) */
Ipp32s mp3_frequency[3][4] = { {22050, 24000, 16000}, {44100, 48000, 32000, 0}, {11025, 12000,  8000} };

/******************************************************************************/

const Ipp32s mp3_mc_pred_coef_table[6][16] = {
  {6, 4, 4, 4, 2, 2, 2, 0, 2, 2, 2, 0, 0, 0, 0, 0},
  {4, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  {2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  {4, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  {2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
};

const Ipp8u mp3_mc_sb_group[32] = {
  0,  1,  2,  3,  4,  5,  6,  7,
  8,  8,  9,  9, 10, 10, 10, 10,
 11, 11, 11, 11, 11, 11, 11, 11,
 11, 11, 11, 11, 11, 11, 11, 11,
};

/******************************************************************************/
/* low-pass 1/96 */
const Ipp32f mp3_lfe_filter[480] = {
  +7.509196822e-004f, +8.199489817e-004f, +8.909908561e-004f, +9.639477099e-004f,
  +1.038713322e-003f, +1.115172702e-003f, +1.193202020e-003f, +1.272668891e-003f,
  +1.353431806e-003f, +1.435340915e-003f, +1.518237668e-003f, +1.601955107e-003f,
  +1.686317716e-003f, +1.771141996e-003f, +1.856236035e-003f, +1.941400367e-003f,
  +2.026427679e-003f, +2.111103392e-003f, +2.195205509e-003f, +2.278505333e-003f,
  +2.360767467e-003f, +2.441750667e-003f, +2.521206845e-003f, +2.598883637e-003f,
  +2.674522118e-003f, +2.747860228e-003f, +2.818630204e-003f, +2.886561145e-003f,
  +2.951379016e-003f, +3.012806356e-003f, +3.070563711e-003f, +3.124369914e-003f,
  +3.173942090e-003f, +3.218997646e-003f, +3.259252850e-003f, +3.294425395e-003f,
  +3.324233545e-003f, +3.348397561e-003f, +3.366640552e-003f, +3.378687913e-003f,
  +3.384269599e-003f, +3.383119271e-003f, +3.374976865e-003f, +3.359586881e-003f,
  +3.336701229e-003f, +3.306078667e-003f, +3.267486221e-003f, +3.220699470e-003f,
  +3.165502835e-003f, +3.101691858e-003f, +3.029072346e-003f, +2.947461228e-003f,
  +2.856688549e-003f, +2.756596332e-003f, +2.647041143e-003f, +2.527892663e-003f,
  +2.399036830e-003f, +2.260374266e-003f, +2.111821990e-003f, +1.953314131e-003f,
  +1.784801926e-003f, +1.606255007e-003f, +1.417661395e-003f, +1.219028364e-003f,
  +1.010382932e-003f, +7.917722211e-004f, +5.632640632e-004f, +3.249473565e-004f,
  +7.693256419e-005f, -1.806479648e-004f, -4.476395677e-004f, -7.238650189e-004f,
  -1.009124173e-003f, -1.303193787e-003f, -1.605827127e-003f, -1.916754149e-003f,
  -2.235680855e-003f, -2.562289578e-003f, -2.896238842e-003f, -3.237163075e-003f,
  -3.584673034e-003f, -3.938355526e-003f, -4.297773405e-003f, -4.662466708e-003f,
  -5.031951239e-003f, -5.405720555e-003f, -5.783243694e-003f, -6.163969158e-003f,
  -6.547321499e-003f, -6.932704737e-003f, -7.319500647e-003f, -7.707071048e-003f,
  -8.094757222e-003f, -8.481879354e-003f, -8.867741087e-003f, -9.251625533e-003f,
  -9.632798125e-003f, -1.001051061e-002f, -1.038399363e-002f, -1.075246699e-002f,
  -1.111513225e-002f, -1.147118184e-002f, -1.181979222e-002f, -1.216012960e-002f,
  -1.249134990e-002f, -1.281259993e-002f, -1.312301737e-002f, -1.342173421e-002f,
  -1.370787557e-002f, -1.398056089e-002f, -1.423890846e-002f, -1.448203315e-002f,
  -1.470904754e-002f, -1.491906765e-002f, -1.511120834e-002f, -1.528458791e-002f,
  -1.543833263e-002f, -1.557156878e-002f, -1.568343403e-002f, -1.577307406e-002f,
  -1.583964366e-002f, -1.588230788e-002f, -1.590024661e-002f, -1.589265456e-002f,
  -1.585874013e-002f, -1.579772884e-002f, -1.570886786e-002f, -1.559141807e-002f,
  -1.544466884e-002f, -1.526792783e-002f, -1.506052776e-002f, -1.482182758e-002f,
  -1.455121023e-002f, -1.424809055e-002f, -1.391191078e-002f, -1.354214280e-002f,
  -1.313829158e-002f, -1.269989631e-002f, -1.222652696e-002f, -1.171779230e-002f,
  -1.117333644e-002f, -1.059283888e-002f, -9.976020162e-003f, -9.322639034e-003f,
  -8.632494749e-003f, -7.905428188e-003f, -7.141321867e-003f, -6.340101074e-003f,
  -5.501735011e-003f, -4.626235371e-003f, -3.713661180e-003f, -2.764114812e-003f,
  -1.777744975e-003f, -7.547465757e-004f, +3.046390356e-004f, +1.400123600e-003f,
  +2.531372163e-003f, +3.698002006e-003f, +4.899583683e-003f, +6.135640408e-003f,
  +7.405648061e-003f, +8.709034615e-003f, +1.004518242e-002f, +1.141342705e-002f,
  +1.281305618e-002f, +1.424331358e-002f, +1.570339624e-002f, +1.719245553e-002f,
  +1.870960062e-002f, +2.025389504e-002f, +2.182435899e-002f, +2.341997389e-002f,
  +2.503967439e-002f, +2.668236092e-002f, +2.834689055e-002f, +3.003208389e-002f,
  +3.173672273e-002f, +3.345955466e-002f, +3.519928849e-002f, +3.695460791e-002f,
  +3.872416015e-002f, +4.050655819e-002f, +4.230038538e-002f, +4.410421362e-002f,
  +4.591656468e-002f, +4.773595571e-002f, +4.956086741e-002f, +5.138977133e-002f,
  +5.322111163e-002f, +5.505331882e-002f, +5.688480512e-002f, +5.871396910e-002f,
  +6.053920474e-002f, +6.235888778e-002f, +6.417138940e-002f, +6.597507620e-002f,
  +6.776830569e-002f, +6.954944448e-002f, +7.131684092e-002f, +7.306886620e-002f,
  +7.480388237e-002f, +7.652026060e-002f, +7.821638576e-002f, +7.989064271e-002f,
  +8.154143914e-002f, +8.316718727e-002f, +8.476633585e-002f, +8.633732449e-002f,
  +8.787862931e-002f, +8.938875381e-002f, +9.086621058e-002f, +9.230953963e-002f,
  +9.371733570e-002f, +9.508819352e-002f, +9.642074433e-002f, +9.771367414e-002f,
  +9.896567805e-002f, +1.001754968e-001f, +1.013419259e-001f, +1.024637791e-001f,
  +1.035399339e-001f, +1.045692864e-001f, +1.055507961e-001f, +1.064834685e-001f,
  +1.073663364e-001f, +1.081985236e-001f, +1.089791543e-001f, +1.097074345e-001f,
  +1.103826251e-001f, +1.110040234e-001f, +1.115709999e-001f, +1.120829523e-001f,
  +1.125393696e-001f, +1.129397681e-001f, +1.132837464e-001f, +1.135709303e-001f,
  +1.138010461e-001f, +1.139738382e-001f, +1.140891334e-001f, +1.141468037e-001f,
  +1.141468037e-001f, +1.140891334e-001f, +1.139738382e-001f, +1.138010461e-001f,
  +1.135709303e-001f, +1.132837464e-001f, +1.129397681e-001f, +1.125393696e-001f,
  +1.120829523e-001f, +1.115709999e-001f, +1.110040234e-001f, +1.103826251e-001f,
  +1.097074345e-001f, +1.089791543e-001f, +1.081985236e-001f, +1.073663364e-001f,
  +1.064834685e-001f, +1.055507961e-001f, +1.045692864e-001f, +1.035399339e-001f,
  +1.024637791e-001f, +1.013419259e-001f, +1.001754968e-001f, +9.896567805e-002f,
  +9.771367414e-002f, +9.642074433e-002f, +9.508819352e-002f, +9.371733570e-002f,
  +9.230953963e-002f, +9.086621058e-002f, +8.938875381e-002f, +8.787862931e-002f,
  +8.633732449e-002f, +8.476633585e-002f, +8.316718727e-002f, +8.154143914e-002f,
  +7.989064271e-002f, +7.821638576e-002f, +7.652026060e-002f, +7.480388237e-002f,
  +7.306886620e-002f, +7.131684092e-002f, +6.954944448e-002f, +6.776830569e-002f,
  +6.597507620e-002f, +6.417138940e-002f, +6.235888778e-002f, +6.053920474e-002f,
  +5.871396910e-002f, +5.688480512e-002f, +5.505331882e-002f, +5.322111163e-002f,
  +5.138977133e-002f, +4.956086741e-002f, +4.773595571e-002f, +4.591656468e-002f,
  +4.410421362e-002f, +4.230038538e-002f, +4.050655819e-002f, +3.872416015e-002f,
  +3.695460791e-002f, +3.519928849e-002f, +3.345955466e-002f, +3.173672273e-002f,
  +3.003208389e-002f, +2.834689055e-002f, +2.668236092e-002f, +2.503967439e-002f,
  +2.341997389e-002f, +2.182435899e-002f, +2.025389504e-002f, +1.870960062e-002f,
  +1.719245553e-002f, +1.570339624e-002f, +1.424331358e-002f, +1.281305618e-002f,
  +1.141342705e-002f, +1.004518242e-002f, +8.709034615e-003f, +7.405648061e-003f,
  +6.135640408e-003f, +4.899583683e-003f, +3.698002006e-003f, +2.531372163e-003f,
  +1.400123600e-003f, +3.046390356e-004f, -7.547465757e-004f, -1.777744975e-003f,
  -2.764114812e-003f, -3.713661180e-003f, -4.626235371e-003f, -5.501735011e-003f,
  -6.340101074e-003f, -7.141321867e-003f, -7.905428188e-003f, -8.632494749e-003f,
  -9.322639034e-003f, -9.976020162e-003f, -1.059283888e-002f, -1.117333644e-002f,
  -1.171779230e-002f, -1.222652696e-002f, -1.269989631e-002f, -1.313829158e-002f,
  -1.354214280e-002f, -1.391191078e-002f, -1.424809055e-002f, -1.455121023e-002f,
  -1.482182758e-002f, -1.506052776e-002f, -1.526792783e-002f, -1.544466884e-002f,
  -1.559141807e-002f, -1.570886786e-002f, -1.579772884e-002f, -1.585874013e-002f,
  -1.589265456e-002f, -1.590024661e-002f, -1.588230788e-002f, -1.583964366e-002f,
  -1.577307406e-002f, -1.568343403e-002f, -1.557156878e-002f, -1.543833263e-002f,
  -1.528458791e-002f, -1.511120834e-002f, -1.491906765e-002f, -1.470904754e-002f,
  -1.448203315e-002f, -1.423890846e-002f, -1.398056089e-002f, -1.370787557e-002f,
  -1.342173421e-002f, -1.312301737e-002f, -1.281259993e-002f, -1.249134990e-002f,
  -1.216012960e-002f, -1.181979222e-002f, -1.147118184e-002f, -1.111513225e-002f,
  -1.075246699e-002f, -1.038399363e-002f, -1.001051061e-002f, -9.632798125e-003f,
  -9.251625533e-003f, -8.867741087e-003f, -8.481879354e-003f, -8.094757222e-003f,
  -7.707071048e-003f, -7.319500647e-003f, -6.932704737e-003f, -6.547321499e-003f,
  -6.163969158e-003f, -5.783243694e-003f, -5.405720555e-003f, -5.031951239e-003f,
  -4.662466708e-003f, -4.297773405e-003f, -3.938355526e-003f, -3.584673034e-003f,
  -3.237163075e-003f, -2.896238842e-003f, -2.562289578e-003f, -2.235680855e-003f,
  -1.916754149e-003f, -1.605827127e-003f, -1.303193787e-003f, -1.009124173e-003f,
  -7.238650189e-004f, -4.476395677e-004f, -1.806479648e-004f, +7.693256419e-005f,
  +3.249473565e-004f, +5.632640632e-004f, +7.917722211e-004f, +1.010382932e-003f,
  +1.219028364e-003f, +1.417661395e-003f, +1.606255007e-003f, +1.784801926e-003f,
  +1.953314131e-003f, +2.111821990e-003f, +2.260374266e-003f, +2.399036830e-003f,
  +2.527892663e-003f, +2.647041143e-003f, +2.756596332e-003f, +2.856688549e-003f,
  +2.947461228e-003f, +3.029072346e-003f, +3.101691858e-003f, +3.165502835e-003f,
  +3.220699470e-003f, +3.267486221e-003f, +3.306078667e-003f, +3.336701229e-003f,
  +3.359586881e-003f, +3.374976865e-003f, +3.383119271e-003f, +3.384269599e-003f,
  +3.378687913e-003f, +3.366640552e-003f, +3.348397561e-003f, +3.324233545e-003f,
  +3.294425395e-003f, +3.259252850e-003f, +3.218997646e-003f, +3.173942090e-003f,
  +3.124369914e-003f, +3.070563711e-003f, +3.012806356e-003f, +2.951379016e-003f,
  +2.886561145e-003f, +2.818630204e-003f, +2.747860228e-003f, +2.674522118e-003f,
  +2.598883637e-003f, +2.521206845e-003f, +2.441750667e-003f, +2.360767467e-003f,
  +2.278505333e-003f, +2.195205509e-003f, +2.111103392e-003f, +2.026427679e-003f,
  +1.941400367e-003f, +1.856236035e-003f, +1.771141996e-003f, +1.686317716e-003f,
  +1.601955107e-003f, +1.518237668e-003f, +1.435340915e-003f, +1.353431806e-003f,
  +1.272668891e-003f, +1.193202020e-003f, +1.115172702e-003f, +1.038713322e-003f,
  +9.639477099e-004f, +8.909908561e-004f, +8.199489817e-004f, +7.509196822e-004f,
};

/******************************************************************************/

Ipp32s mp3_SetAllocTable(Ipp32s header_id, Ipp32s mpg25, Ipp32s header_layer,
                         Ipp32s header_bitRate, Ipp32s header_samplingFreq,
                         Ipp32s stereo,
                         Ipp32s **nbal_alloc_table,
                         Ipp8u  **alloc_table,
                         Ipp32s *sblimit)
{
  Ipp32s bit_rate = mp3_bitrate[header_id][header_layer - 1][header_bitRate] >> (stereo - 1);
  Ipp32s freq = mp3_frequency[header_id + mpg25][header_samplingFreq];

  if (header_id == 0) {
    *alloc_table = mp3_alloc_table5;
    *nbal_alloc_table = mp3_nbal_alloc_table5;
    *sblimit = mp3_sblimit_table[4];
  } else if (bit_rate <= 48) {
    if (freq == 32000) {
      *alloc_table = mp3_alloc_table4;
      *nbal_alloc_table = mp3_nbal_alloc_table4;
      *sblimit = mp3_sblimit_table[3];
    } else {
      *alloc_table = mp3_alloc_table3;
      *nbal_alloc_table = mp3_nbal_alloc_table3;
      *sblimit = mp3_sblimit_table[2];
    }
  } else if (bit_rate <= 80) {
    *alloc_table = mp3_alloc_table1;
    *nbal_alloc_table = mp3_nbal_alloc_table1;
    *sblimit = mp3_sblimit_table[0];
  } else if (bit_rate <= 192) {
    if (freq == 48000) {
      *alloc_table = mp3_alloc_table1;
      *nbal_alloc_table = mp3_nbal_alloc_table1;
      *sblimit = mp3_sblimit_table[0];
    } else {
      *alloc_table = mp3_alloc_table2;
      *nbal_alloc_table = mp3_nbal_alloc_table2;
      *sblimit = mp3_sblimit_table[1];
    }
  } else {
    return 0;
  }
  return 1;
}

/******************************************************************************/

#endif //UMC_ENABLE_XXX
